<?php
@session_start(); // khoi tao session

require_once ("../../model/invoice.php");
$obj_invoice = new Invoice();

header("Content-type: text/html;charset=utf-8");

$username = isset($_SESSION['username'])? $_SESSION['username'] : null;
$sum_total = isset($_SESSION['tong_tien']) ? $_SESSION['tong_tien'] : 0;

if($username) {
    $sum_total = $sum_total * 0.85;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="stylesheet" type="text/css" href="style_payment.css"/>
    <link rel="stylesheet" type="text/css" href="../../assets/css/style.css"/>
</head>
<body>
    <div id="wrapper">
        <fieldset>
            <legend>Thanh toán giỏ hàng (Giảm giá 15% tổng hàng cho user)</legend>
            <form name="form1">
                <label>Mã hóa đơn:</label>
                <input type="text" name="invoice_code" value="<?php echo "HHD".rand(0,999);?>"/> <br/>
                <label>Tổng tiền:</label>
                <input type="text" value="<?=$sum_total?>$" name="sum_total"/> <br>
                <label>Người mua hàng:</label>
                <input type="text" value="<?=$username?>" name="username"/><br/>
                <label>Email:</label>
                <input type="text" name="email" placeholder="email"/> <br/>
                <label>Địa chỉ:</label>
                <input type="text" name="address" placeholder="address"/> <br/>
                <label>Ngân hàng:</label>
                <select name="banking">
                    <option value="">Lựa chọn ngân hàng</option>
                    <option value="viettin-bank">Viettin-Bank</option>
                    <option value="vietcom-bank">Vietcom-Bank</option>
                    <option value="Miltary-bank">Miltary-Bank</option>
                    <option value="VP-bank">VP-Bank</option>
                    <option value="MB-bank">MB-Bank</option>
                </select> <br />
                <label>Nhập số tài khoản:</label>
                <input type="text" name="bank-code" placeholder="bank-code"/> <br />
                <label></label>
                <br />
                <br />
                <br />
                <br />
                <input type="button" value="Pay" onclick="payment()"/>
                <input type="button" value="Xem hóa đơn" onclick="show_detail_invoice()"/>
            </form>
        </fieldset> <!-- END fieldset -->
    </div>
</body>
</html>
<script src="../../assets/jquery-1.10.2.js" type="text/javascript"></script>
<script type="text/javascript" lang="javascript">
    function show_detail_invoice()
    {
        // $("#wrapper").html(""); // khoi tao rong cho wrapper
        $.ajax({
            url: "invoice.php",
            type: "POST",
            data: "",
            dataType: "json",
            success: function(response, status)
            {
                console.log(status);  // hien thi thong bao loi
                console.log(response);
                var html = "<table border=\"0\" class='table table-striped'>";
                html += "<thead>";
                    html += "<th colspan='2'>Customername: " + $("form :text[name='username']").val() +"</th>";
                    html += "<th colspan='2'>Invoice code: " + $("form :text[name='invoice_code']").val() + "</th>";
                html += "</thead>";

                html += "<tr>";
                    html += "<th>Tên ứng dụng</th>";
                    html += "<th>Giá tiền</th>";
                    html += "<th>Số lượng</th>";
                    html += "<th>Thành tiền</th>";
                html += "</tr>";
                var sum_total = 0;
                $.each(response, function(key, value){
                    html += "<tr>";
                        html += "<td>" + value.ten_app + "</td>";
                        html += "<td>" + value.gia_app + "<font color='green'><b>$</b></font></td>";
                        html += "<td>" + value.so_luong + "</td>";
                        html += "<td>" + value.so_luong* value.gia_app + "<font color='green'><b>$</b></font></td>";
                        sum_total += value.so_luong* value.gia_app;
                    html += "</tr>";
                });
                html += "<tr>";
                    html += "<td colspan='4'>Tổng tiền: " + sum_total + "<font color='green'><b>$</b></font></td>";
                html += "</tr>";
                html += "</table>";
                $("#wrapper").html(html);
            }
        });
    }

    // luu thong tin hoa don sau khi payment
    function save_invoice_into_table(invoice_id, username, email, address, sum_total)
    {
        $.ajax({
            url : "../../controller/controller_saveInvoice.php",
            data : {
                "invoice_id": invoice_id,
                "username" : username,
                "email" : email,
                "address": address,
                "sum_total": sum_total
            },
            dataType: "json",
            type: "POST",
            success : function(response) {
                console.log(response);
            }

        });
    }
    function payment()
    {
        var invoice_id = document.form1.invoice_code;
        var username  = document.form1.username;
        var sum_total = document.form1.sum_total;
        var email     = document.form1.email;
        var address   = document.form1.address;

        if(username.value == "")
        {
            alert("Mời bạn nhập tên");
            username.focus();
            return false;
        }

        if(email.value == "")
        {
            alert("Mời bạn nhập email của bạn");
            email.focus();
            return false;
        }

        if(address.value == "")
        {
            alert("Mời bạn nhập vào địa chỉ");
            address.focus();
            return false;
        }

        alert("Thank you for pay");

        save_invoice_into_table(invoice_id.value, username.value, email.value, address.value, sum_total.value);
    }
</script>